using System;
using System.Windows.Controls;
using System.Windows;


namespace Plugins.Windows
{
    public class TreeListView : TreeView
    {
        #region Variables
        
        private GridViewColumnCollection _columns = new GridViewColumnCollection();

        private event EventHandler<TreeListViewItemEventArgs> BrakePointChanged;
        public EventHandler<TreeListViewItemEventArgs> BrakePointChangedHandler;

        #endregion

        public TreeListView() : base()
        {            
            BrakePointChangedHandler = new EventHandler<TreeListViewItemEventArgs>(OnBrakePointChanged);            
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new TreeListViewItem();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is TreeListViewItem;
        }

        /// <summary> GridViewColumn List</summary>
        public GridViewColumnCollection Columns
        {
            get
            {
                if (_columns == null)
                    _columns = new GridViewColumnCollection();
                
                return _columns;
            }
        }
        
        /// <summary>
        /// Expand all elements in Tree
        /// </summary>
        public void ExpandAll()
        {
            if (Items != null)
                foreach (TreeListViewItem item in Items)
                    Expand_Collapse_Recursive(item, true);                
        }

        /// <summary>
        /// Expand/Colapse all elements start with item and its childs - recursive.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="expand"></param>
        private void Expand_Collapse_Recursive(TreeListViewItem item, bool expand) 
        {
            if (item == null)
                return;

            item.IsExpanded = expand;

            if (item.HasItems)
                foreach (TreeListViewItem child in item.Items)                
                    Expand_Collapse_Recursive(child, expand);                
        }

        /// <summary>
        /// Collapse all elements in Tree
        /// </summary>
        public void CollapseAll()
        {
            if (Items != null)
                foreach (TreeListViewItem item in Items)                
                    Expand_Collapse_Recursive(item, false);                
        }

        /// <summary>
        /// Delegate method for BrakePointButton.Click.  Raises BrakePointChanged event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnBrakePointChanged(object sender, TreeListViewItemEventArgs e)
        {
            if (BrakePointChanged != null)
                BrakePointChanged(sender, e);            
        }
    }
}
